package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author kaixuan
 * @since 2020-05-30
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    //注入service-vod服务对象
    @Autowired
    private VodClient vodClient;

    /**
     * 根据课程id删除小节   删除视频
     * @param courseId
     * @return
     */
    @Override
    public void removeVideo(String courseId) {
        //删除视频
        //根据课程id查询所有视频id
        QueryWrapper<EduVideo> query = new QueryWrapper<>();
        query.eq("course_id",courseId);
        List<EduVideo> videoList= baseMapper.selectList(query);
        //获取视频id的非空集合
        List<String> videoIdList = videoList.stream().map(EduVideo::getVideoSourceId).filter(x -> x !=null ).collect(Collectors.toList());
//        List<String> videoIdList = videoList.stream().map(EduVideo::getVideoSourceId ).collect(Collectors.toList());
        if (null != videoIdList && videoIdList.size() > 0){
            vodClient.deleteVideoList(videoIdList);
        }
        //删除小节信息
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
